import request from "../utils/request.ts";
import {message} from 'antd';
import type {AxiosResponse} from 'axios'


//获取账号列表
export const getAccountList = async (params: any): Promise<any> => {
    try {
        const res: AxiosResponse<any, any> = await request.get('/users/list', params)
        if (res.data.code === 1) {
            return res.data
        }
        message.error(res.data.msg)
    } catch (err) {
        message.error('获取账号列表失败')
        throw err
    }
}

//批量删除账号
export const delAllAccount = async (data: any): Promise<any> => {
    try {
        const res: AxiosResponse<any, any> = await request.post('/users/batchdel', data)
        if (res.data.code === 1) {
            message.success(res.data.msg)
            return res.data
        }
        message.error(res.data.msg)
    } catch (err) {
        message.error('批量删除账号失败')
        throw err
    }
}

// 删除账号
export const delAccount = async (params: any): Promise<any> => {
    try {
        const res: AxiosResponse<any, any> = await request.get('/users/del', params)
        if (res.data.code === 1) {
            message.success(res.data.msg)
            return res.data
        }
        message.error(res.data.msg)
    } catch (err) {
        message.error('删除账号失败')
        throw err
    }
}

//获取账号详情
export const getAccountDetails = async (params: any): Promise<any> => {
    try {
        const res: AxiosResponse<any, any> = await request.get('/users/getbyid', params)
        if (res.data.code === 1) {
            return res.data
        }
        message.error(res.data.msg)
    } catch (err) {
        message.error('获取帐号详情失败')
        throw err
    }
}


//根据姓名查询账号
export const getAccountSearch = async (params: any): Promise<any> => {
    try {
        const res: AxiosResponse<any, any> = await request.get('/users/search', params)
        if (res.data.code === 1) {
            return res.data
        }
        message.error(res.data.msg)
    } catch (err) {
        message.error('查询账号失败')
        throw err
    }
}


//修改账号
export const editAccount = async (params: any): Promise<any> => {
    try {
        const res: AxiosResponse<any, any> = await request.get('/users/edit', params)
        if (res.data.code === 1) {
            message.success(res.data.msg)
            return res.data
        }
        message.error(res.data.msg)
    } catch (err) {
        message.error('修改账号失败')
        throw err
    }
}


//添加账号
export const addAccount = async (data: any): Promise<any> => {
    try {
        const res: AxiosResponse<any, any> = await request.post('/users/add', data)
        if (res.data.code === 1) {
            message.success(res.data.msg)
            return res.data
        }
        message.error(res.data.msg)
    } catch (err) {
        message.error('添加账号失败')
        throw err
    }
}